﻿Imports MySql.Data.MySqlClient
Imports conn
Public Class Main_Login
    ''' <summary>
    ''' Possiblely move the connection check to the connection function for checks.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub OK_Click() Handles OK.Click
        conn.connection.conn()
        Dim command = New MySqlCommand
        command.CommandText = "SELECT * FROM `members` WHERE `username` = '" & UsernameTextBox.Text.ToLower & "' AND `password` = '" & functions.getMd5Hash(PasswordTextBox.Text) & "' " 'Query to exectue.
        command.Connection = connection.MyADOConnection
        Dim read = command.ExecuteReader
        read.Read() 'Opens the reader.
        If read.HasRows Then
            details.username = read.GetString(1) 'Gets the data from the database
            details.name = read.GetString(5) & " " & read.GetString(6)
            details.money = read.GetString(4)
            read.Close() 'Closes the reader
            conn.connection.MyADOConnection.Close() 'Closes the database connection
            Welcome.Show()
            Me.Close()
        Else
            MsgBox("Sorry there has been a problem logging you in.", MsgBoxStyle.Critical)
            MsgBox("Check your details and try again.", MsgBoxStyle.Critical)
            read.Close() 'Closes the reader
            connection.MyADOConnection.Close() 'Closes the database connection
        End If
    End Sub
    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        End
    End Sub
    Private Sub Main_Login_Load() Handles MyBase.Load
        If My.User.Name = "BENS\Ben Burt" Then
            btnburtylogin.Visible = True
        ElseIf My.User.Name = "Steve-PC\Steve" Then
            btnstevelogin.Visible = True
        End If
        If connection.conn() Then
            connection.MyADOConnection.Close()
        Else
            MsgBox("Sorry there was an error connecting to the server. Please try again later", MsgBoxStyle.Critical, "ERROR")
            UsernameTextBox.Enabled = False
            PasswordTextBox.Enabled = False
            OK.Enabled = False
        End If

    End Sub
    Private Sub btnburtylogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnburtylogin.Click
        UsernameTextBox.Text = "burtybob"
        PasswordTextBox.Text = "benburtabc123"
        OK_Click()
    End Sub
    Private Sub btnstevelogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstevelogin.Click
        UsernameTextBox.Text = "SSaywell"
        PasswordTextBox.Text = "3050"
        OK_Click()
    End Sub
    Private Sub UsernameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsernameTextBox.TextChanged
        If UsernameTextBox.Text <> "burtybob" Or UsernameTextBox.Text <> "ssaywell" Then
            btnburtylogin.Visible = False
            btnstevelogin.Visible = False
        End If
    End Sub
End Class
